﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using RFIDLIB;
using wareHouse;

namespace ShuoXiangProductionPlatform
{
    public partial class ZhuangTi : Form
    {
        public ZhuangTi()
        {
            InitializeComponent();
           
            
            for (int i = 1; i < 11; i++)
            {
                comboBox1.Items.Add(i);
            }
           // comboBox1.SelectedIndex = 0;
            this.groupBox3.Hide();
            //初始化RFID，连接读卡器
            RfidNetHelper.IninTeaRead();
            if (RfidNetHelper.OpenTeaReader(url3))
            {
                this.label12.Text = "设备连接成功";
            }
            else
            {
                this.label12.Text = "设备连接失败";
            }
            RfidNetHelper.StartTeaRead();
            RfidNetHelper.OngetTagDatas += new RfidNetHelper.GetTagDatas(TeaRfidHelper_OngetTagDatas);
            comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
        }

        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            qrcodelist.Clear();
            numscan = 0;
            num = Convert.ToInt32(comboBox1.SelectedItem);
        }

        //网口连接方式
        string url3 = "RDType=RD5100;CommType=NET;RemoteIP=192.168.0.222;RemotePort=9909;LocalIP=";

        public bool isstart = false;

        public List<string> qrcodelist = new List<string>();

        //需要扫描的数量
        public int num = 0;
        //已经成功扫描的数量
        public int numscan = 0;

        public bool isscan = false;

        public bool ischange = false;

        public delegate void ShowTag(string tagInfo);

        private void TeaRfidHelper_OngetTagDatas(string tagInfo)
        {
            if (InvokeRequired)
            {
                Invoke(new ShowTag(TeaRfidHelper_OngetTagDatas), new object[] { tagInfo });
                return;
            }
            if (isstart == true)
            {
                string[] arrs = tagInfo.Split(',');
                if (arrs.Length > 1 && arrs[0].Split(':').Length < 2)
                {
                    string uid = arrs[1];
                    //重复扫码
                    if (qrcodelist.Contains(uid))
                    {

                    }
                    else
                    {
                        if (num == numscan)
                        {
                          
                        }
                        else
                        {
                            qrcodelist.Add(uid);
                            numscan++;
                            this.label4.Text = numscan.ToString();
                            if (num == numscan)
                            {
                                isscan = true;
                                isstart = false;
                            }
                        }
                    }
                }
            }
        }


        public void StartScan(string qrcode)
        {
            if (isscan == true)
            {
                this.label6.Text = qrcode;
                this.label5.Text = "扫描成功";
                if (qrcodelist.Count == num)
                {
                    BoxInfo boxInfo = new BoxInfo { qrcode = qrcode, userId = Login.login.userInfo.logindata.userId, qrCodeList = qrcodelist };
                    string param = JsonConvert.SerializeObject(boxInfo);
                    HttpHelper httpHelper = new HttpHelper();
                    string res = httpHelper.HTTPJsonPost("http://47.102.195.242:28083/qrcode/submit/p", param);
                    var resjson = JsonConvert.DeserializeObject<JsonString.ApiMsgList<JsonString.orderlistdata>>(res);
                    if (resjson.success.Equals("T"))
                    {
                        label11.Text = "装提成功";
                        this.groupBox3.Show();
                        this.timer1.Enabled = true;
                        this.timer1.Interval = 2000;
                        this.timer1.Tick += Timer1_Tick;
                    }
                    else
                    {
                        label11.Text ="装提失败:"+ resjson.msg;                      
                        this.groupBox3.Show();
                        this.timer1.Enabled = true;
                        this.timer1.Interval = 2000;
                        this.timer1.Tick += Timer1_Tick;
                    }
                  
                }
                qrcodelist.Clear();
                
                this.label5.Text = "等待扫描提标";
                this.label4.Text = "0";
                numscan = 0;
                qrcodelist.Clear();
                isstart = true;
                isscan = false;
            }
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
            this.label6.Text = "";
            this.groupBox3.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (ischange == false)
            {
                isstart = true;
                this.button1.Text = "暂停";
                this.comboBox1.Enabled = false;
                ischange = true;

            }
            else
            {
                ischange = false;
                isstart = false;
                this.button1.Text = "开始";
                this.comboBox1.Enabled = true;
            }

        }
    }
}
